<template>
  <div class="store-shelf">
    <shelf-title :title="$t('shelf.title')" @showMyToast="showMyToast"></shelf-title>
    <scroll class="store-shelf-scroll-wrapper"
            :top="0"
            :bottom="scrollBottom"
            @onScroll="onScroll"
            ref="scroll">
      <shelf-search></shelf-search>
      <shelf-list :data="shelfList"></shelf-list>
    </scroll>
    <shelf-footer @showMyToast="showMyToast"></shelf-footer>
    <Toast v-if="showToast" :text="toastText" />
  </div>
</template>

<script>
  import { storeShelfMixin } from '../../utils/mixin'
  import ShelfTitle from '../../components/shelf/ShelfTitle'
  import ShelfSearch from '../../components/shelf/ShelfSearch'
  import ShelfList from '../../components/shelf/ShelfList'
  import ShelfFooter from '../../components/shelf/ShelfFooter'
  import Scroll from '../../components/common/Scroll'
  import Toast from '../../components/shelf/Toast'
  import { shelf } from '../../api/store'
  import { appendAddToShelf, removeAddFromShelf } from '../../utils/store'

  export default {
    mixins: [storeShelfMixin],
    components: {
      Scroll,
      ShelfTitle,
      ShelfSearch,
      ShelfList,
      ShelfFooter,
      Toast
    },
    watch: {
      // 监听isEdited变化，因为书架底部出现时，scroll滚动条增加距离下方的距离，不再全屏显示
      isEditMode(isEditMode) {
        this.scrollBottom = isEditMode ? 96 : 0
        // 等界面相应完了后，才更新
        this.$nextTick(() => {
          this.$refs.scroll.refresh()
        })
      },
      showToast(newVal) {
        // console.log('newVal', newVal)
        if (newVal) {
          this.showTimer = setTimeout(() => {
            this.showToast = false
            this.toastText = ''
          }, 3000)
        }
      },
      // 持续显示toast文本
      toastText(newVal, oldVal) {
        // console.log('oldVal', oldVal)
        // 清除延时器
        if (oldVal !== '') {
          clearTimeout(this.showTimer)
          if (newVal === '已开启离线' || newVal === 'Opened, will automatically download') {
            // console.log('开启延时器')
            this.showTimer = setTimeout(() => {
              this.toastText = ''
              this.showToast = false
            }, 1000)
          }
        }
      }
    },
    data() {
      return {
        scrollBottom: 0,
        showToast: false,
        toastText: '',
        showTimer: null,
        myShelfList: []
      }
    },
    methods: {
      onScroll(offsetY) {
        this.setOffsetY(offsetY)
      },
      showMyToast(text) {
        // console.log('showMyToast', text)
        this.showToast = true
        this.toastText = text
      }
    },
    mounted() {
      this.getShelfList()
      // /因为现在处于书架列表，所以ShelfCategory就为空，ShelfCategory分组列表
      this.setShelfCategory([])
      // 表示当前处书架列表
      this.setCurrentType(1)
    }
  }
</script>

<style lang="scss" rel="stylesheet/scss" scoped>
  @import "../../assets/styles/global";
  .store-shelf {
    position: relative;
    z-index: 100;
    width: 100%;
    height: 100%;
    background: white;
    .store-shelf-scroll-wrapper {
      position: absolute;
      top: 0;
      left: 0;
      z-index: 101;
    }
  }
</style>
